package org.wiw.client;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.wiw.client.model.Person;
import org.wiw.client.model.Place;

import com.google.gwt.maps.client.overlay.Marker;

public class MarkerPlaceStorage {
	
	private Map<Marker, Place>marker2place = new HashMap<Marker, Place>();
	private Map<Place, Marker>place2marker = new HashMap<Place, Marker>();
	private Map<Long,Person> persons = new HashMap<Long,Person>();
	private Map<Long,Integer> personsPlaces = new HashMap<Long,Integer>();
	
	public void put(Marker marker, Place place){
		marker2place.put(marker, place);
		place2marker.put(place, marker);
		long personId = place.getPerson().getId();
		persons.put(personId,place.getPerson());
		
		if(personsPlaces.containsKey(personId)){
			personsPlaces.put(personId, 1 + personsPlaces.get(personId));
		}
		else{
			personsPlaces.put(personId, 1);
		}
		
	}
	
	public Marker get(Place place){
		return place2marker.get(place);
	}
	
	public Place get(Marker marker){
		return marker2place.get(marker);
	}
	
	public Set<Place> getPlaces(){
		return place2marker.keySet();
	}
	
	public Set<Marker> getMarkers(){
		return marker2place.keySet();
	}
	
	public Collection<Person> getPersons(){
		return persons.values();
	}
	
	public int getPersonPlacesCount(Person person){
		long personId = person.getId();
		if(personsPlaces.containsKey(personId)){
			return personsPlaces.get(personId);
		}
		return 0;
	}
	
	public void remove(Place place){
		Marker marker = get(place);
		place2marker.remove(place);
		marker2place.remove(marker);
		persons.remove(place.getPerson().getId());
		personsPlaces.remove(place.getPerson().getId());
	}
	
	public void remove(Marker marker){
		remove(get(marker));
	}
	
	public void clear(){
		place2marker.clear();
		marker2place.clear();
		persons.clear();
	}
}
